<template>
  <vol-box :lazy="true" v-model="model"  title="所需工具管理" :width="1200" :padding="0" >
    <div>
      <customer ref="customer"></customer>
    </div>
    <template #footer>
      <div> <el-button type="primary"  @click="onSelect">确认</el-button></div></template>
  </vol-box>

  <vol-box :lazy="true" v-model="model2"  title="所需材料" :width="1200" :padding="0" >
    <div>
      <customer2 ref="customer2"></customer2>
    </div>
    <template #footer>
      <div> <el-button type="primary"  @click="onSelect2">确认</el-button></div></template>
  </vol-box>
</template>

<script>
import VolBox from '@/components/basic/VolBox.vue';

/******注意：如果出现提示没有权限的问题，见后台开发文档上的【重写后台权限】*****/

//直接引用生成的页面(也可以单独用table配置，单独配置下见下面【编辑弹出框明细表选择数据】)
import customer from '@/views/system/workbusiness/PROJ_ReqTool.vue';//'@/views/system/customer/Demo_Customer';
import customer2 from '@/views/system/workbusiness/PROJ_ReqMaterial.vue';
//这里使用的vue2语法，也可以写成vue3语法
export default {
  components: {
    'vol-box': VolBox,
    customer,
    customer2,
  },
  methods: {},
  data() {
    return {
      sel:'0',
      pararow: {},
      model: false, //弹出框
      model2: false, //弹出框
    };
  },
  methods: {
    open(row,sel) {
       this.sel = sel;     
       console.log('row', row);
        this.pararow = row;
      //打开主表选择数据
      this.model = true;
      
      //打开时刷新界面数据(2022.12.06)
      this.$nextTick(() => {
        debugger;
        this.$refs.customer && this.$refs.customer.$refs.grid.search();
      });
    },
    open2(row,sel) {
       this.sel = sel;     
       console.log('row', row);
        this.pararow = row;
      //打开主表选择数据
      this.model2 = true;
      
      //打开时刷新界面数据(2022.12.06)
      this.$nextTick(() => {
        debugger;
        this.$refs.customer2 && this.$refs.customer2.$refs.grid.search();
      });
    },
    onSelect() {

      //主表的选择数据回写到主表的表单上
      let rows = this.$refs.customer.$refs.grid.getSelectRows();
      if (!rows.length) {
        return this.$message.error('请选择数据');
      }
      this.$emit('parentCall', ($parent) => {
        //如：回写编辑表单数据
        if(this.sel=='1'){
          $parent.editFormFields.RequiredToolName = rows[0].RequiredToolName;
        }else{

             //调用框架的保存方法，或者自己写接口保存
        //      let tools = rows.map((item) => {
        //       return {
        //         RequiredToolName: item.RequiredToolName
        //       };
        // });
        let tools = rows.map(item => item.RequiredToolName);
            //  for (let i = 0; i < rows.length; i++) {
            //   if (rows[i].RequiredToolName == null) {
            //     this.$message.error('所需工具不能为空');
            //     return;
            //   }
            // }
             this.pararow.RequiredTool =tools.join(',');//JSON.stringify(tools.join(',')) ;
             console.log('this.pararow', this.pararow);
          debugger;
              this.http.post('/api/PROJ_KitCheck/update', { mainData: this.pararow }, true)
              .then((x) => {
                if (x.status) {
              this.$message.info('保存成功');
              this.$parent.search();
              return;
              }
              this.$message.error(x.message);
          });
        }
      //  $parent.editFormFields.Version = rows[0].VersionCode;
        this.model = false;
      });
    },
    onSelect2() {

//主表的选择数据回写到主表的表单上
let rows = this.$refs.customer2.$refs.grid.getSelectRows();
if (!rows.length) {
  return this.$message.error('请选择数据');
}
this.$emit('parentCall', ($parent) => {
  //如：回写编辑表单数据
  if(this.sel=='1'){
    $parent.editFormFields.RequiredToolName = rows[0].RequiredToolName;
  }else{

       //调用框架的保存方法，或者自己写接口保存

  let tools = rows.map(item => item.RequiredMaterialName);

       this.pararow.RequiredMaterial =tools.join(',');//JSON.stringify(tools.join(',')) ;
       console.log('this.pararow', this.pararow);
 
        this.http.post('/api/PROJ_KitCheck/update', { mainData: this.pararow }, true)
        .then((x) => {
          if (x.status) {
        this.$message.info('保存成功');
       // this.search();
       debugger;
       this.$parent.search();//刷新父页面
        return;
        }
        this.$message.error(x.message);
    });
  }
//  $parent.editFormFields.Version = rows[0].VersionCode;
  this.model2 = false;
});
},
  }
};
</script>
<style lang="less" scoped>
.search-form {
  display: flex;
  padding: 10px;
  line-height: 34px;
  button {
    margin-left: 10px;
  }
}
</style>
